﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class User : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userid"] == null)
                {
                    Response.Redirect("/Admin");
                }
              
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.txtFname.Text = "";
            this.txtLname.Text = "";
            
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtFname.Text = "";
            this.txtLname.Text = "";
            this.txtGender.SelectedIndex = 0;
            this.txtAddress.Text = "";
            this.txtUserName.Text = "";
            this.txtPassword.Text = "";
            this.txtCPassword.Text = "";
            this.txtUserType.SelectedIndex = 0;
            txtFname.Focus();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String sms;
            var cp = txtCPassword.Text;
            Model.User u = new Model.User();
            u.FirstName = txtFname.Text;
            u.LastName = txtLname.Text;
            u.Gender = txtGender.Text;
            u.Address = txtAddress.Text;
            u.Password = txtPassword.Text;
            u.UserName = txtUserName.Text;
            u.UserType = txtUserType.Text;
            if (u.FirstName == "" || u.UserName == "" || u.Password == "")
            {
                // if some information is blank
                sms = "<div class='alert alert-error'>";
                sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                sms += "Please input all required fields!";
                sms += "</div>";
                this.actionSms.InnerHtml = sms;
            }
            else
            {
                // if the password is not matched
                if (u.Password != cp)
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Missed matched password!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    // if the genter is not selected
                    if (txtGender.SelectedIndex==0)
                    {
                        sms = "<div class='alert alert-error'>";
                        sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                        sms += "Invalid gender. Choose it correctly!";
                        sms += "</div>";
                        this.actionSms.InnerHtml = sms;
                    }
                        // if the user type is not selected
                    else if(txtUserType.SelectedIndex==0)
                    {
                        sms = "<div class='alert alert-error'>";
                        sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                        sms += "Invalid user type. Choose it correctly!";
                        sms += "</div>";
                        this.actionSms.InnerHtml = sms;
                    }
                    else
                    {
                        // if everything is OK, ready to add new user
                        if (u.Add())
                        {
                            sms = "<div class='alert alert-success'>";
                            sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                            sms += "A new user has been added!";
                            sms += "</div>";
                            this.actionSms.InnerHtml = sms;
                            // clear information in form, so it is ready to the new one

                            this.txtFname.Text = "";
                            this.txtLname.Text = "";
                            this.txtGender.SelectedIndex = 0;
                            this.txtAddress.Text = "";
                            this.txtUserName.Text = "";
                            this.txtPassword.Text = "";
                            this.txtCPassword.Text = "";
                            this.txtUserType.SelectedIndex = 0;
                            txtFname.Focus();
                        }
                        else
                        {
                            // if cannot add, show some messages
                            sms = "<div class='alert alert-error'>";
                            sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                            sms += "Cannot add new user! It may already exist.";
                            sms += "</div>";
                            this.actionSms.InnerHtml = sms;
                        }
                    }
                }
                
            }
           
        }

        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            // delete a user
            if (e.CommandName == "delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.User u = new Model.User();
                if(u.Remove(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
            if (e.CommandName=="detail")
            {
                mv.ActiveViewIndex = 3;
                int id = Convert.ToInt32(e.CommandArgument);
                Model.User u = new Model.User().GetUser(id);
                lbUserId.Text = id.ToString();
                lbFname.Text = u.FirstName;
                lbLname.Text = u.LastName;
                lbGender.Text = u.Gender;
                lbAddress.Text = u.Address;
                lbUserName.Text = u.UserName;
                lbUserType.Text = u.UserType;
                lbIsDeleted.Text = u.IsDeleted.ToString() + " (0=not deleted, 1=deleted)";
            }
            if (e.CommandName == "edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                this.txtid.Value = id.ToString();
                this.mv.ActiveViewIndex = 2;
                Model.User u = new Model.User().GetUser(id);
                this.eTxtFname.Text = u.FirstName;
                this.eTxtLname.Text = u.LastName;
                this.eTxtGender.Text = u.Gender;
                this.eTxtAddress.Text = u.Address;
                this.eTxtUserName.Text = u.UserName;
                this.eTxtGender.SelectedIndex = this.eTxtGender.Items.IndexOf(this.eTxtGender.Items.FindByValue(u.Gender));
                this.eTxtUserType.SelectedIndex = this.eTxtUserType.Items.IndexOf(this.eTxtUserType.Items.FindByValue(u.UserType));
            }
        }

        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string sms;
            Model.User u = new Model.User();
            u.Id = Convert.ToInt32(txtid.Value);
            u.FirstName = eTxtFname.Text;
            u.LastName = eTxtLname.Text;
            u.Address = eTxtAddress.Text;
            u.Gender = eTxtGender.SelectedValue.ToString();
            u.UserName = eTxtUserName.Text;
            u.Password = eTxtPassword.Text;
            u.UserType = eTxtUserType.SelectedValue.ToString();

            if (u.FirstName =="" || u.UserName=="")
            {
                sms = "<div class='alert alert-error'>";
                sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                sms += "The required information is missing.";
                sms += "</div>";
                this.updateSms.InnerHtml = sms;
            }
            else
            {
                if(eTxtUserType.SelectedIndex==0 || eTxtGender.SelectedIndex==0)
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "The required information is missing.";
                    sms += "</div>";
                    this.updateSms.InnerHtml = sms;
                }
                else
                {
                    if (u.Password=="")
                    {
                        if (u.UpdateUser(u.Id.ToString(), ""))
                        {
                            this.rp1.DataBind();
                            this.mv.ActiveViewIndex = 0;
                        }
                        else
                        {
                            sms = "<div class='alert alert-error'>";
                            sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                            sms += "Cannot make change to the existing user!";
                            sms += "</div>";
                            this.updateSms.InnerHtml = sms;
                        }
                    }
                    else
                    {
                        if(eTxtCPassword.Text ==u.Password)
                        {
                            if (u.UpdateUser(u.Id.ToString(), u.Password))
                            {
                                this.rp1.DataBind();
                                this.mv.ActiveViewIndex = 0;
                            }
                            else
                            {
                                sms = "<div class='alert alert-error'>";
                                sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                                sms += "Missing information, cannot update!";
                                sms += "</div>";
                                this.updateSms.InnerHtml = sms;
                            }
                        }
                        else
                        {
                            sms = "<div class='alert alert-error'>";
                            sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                            sms += "Missed matched-password!";
                            sms += "</div>";
                            this.updateSms.InnerHtml = sms;
                        }
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }
    }
}